package com.nkzy.servlet;

/**
 * Create by 王建烨 on 2022/4/1 10:57
 **/

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * SpringBoot集成Servlet
 *
 * 原理：创建一个Servlet通过注解@WebServlet
 *
 * 步骤一：在根包下创建子包Servlet
 * 步骤二：新建Servlet类
 * 步骤三：修改Servlet，get，post合二为一，修改请求的路径，合二为一
 * 步骤四：在启动类中添加注解@ServletComponentScan，让启动类扫描到Servlet
 */

@WebServlet("/firstServlet")
public class FirstServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 把Get交给Post处理
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("content-type","text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().println("<h1>嗨害嗨！Second Servlet</h1>");
        System.out.println("嗨害嗨！First Servlet");
    }
}
